//
//  CMDBMovieStaffCollectionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/9.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let MovieStaffCollectionCellID = "MovieStaffCollectionCellID"

private let MovieStaffHeaderID = "MovieStaffHeaderID"

private let KMovieWidth : CGFloat = (CMDB_Screen_Width - (KleftMargin + 12) * 5) / 4

class CMDBMovieStaffCollectionView: BaseCollectionView {
    
    override func commitInitOwnProperties() {
        super.commitInitOwnProperties()
        
        backgroundColor = UIColor.whiteColor()
        
        registerClass(CMDBMovieDetailStaffCollectionCell.self, forCellWithReuseIdentifier: MovieStaffCollectionCellID)
        
        registerClass(CMDBMovieStaffCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: MovieStaffHeaderID)
        
    }
}

extension CMDBMovieStaffCollectionView {

    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSizeMake(KMovieWidth, 35 + KMovieWidth)
    }
    
    override internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(15, 20, 15, 20)
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        mDelegate?.onItemClickType!("CollectionViewCellClick", data: nil, indexPath: indexPath)
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 20
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
       return 20
    }
}

extension CMDBMovieStaffCollectionView{
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 5
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(MovieStaffCollectionCellID, forIndexPath: indexPath) as! CMDBMovieDetailStaffCollectionCell
        cell.mDelegate = self
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionElementKindSectionHeader {
            
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: MovieStaffHeaderID, forIndexPath: indexPath) as! CMDBMovieStaffCollectionHeaderView
            
            return header
            
        } else {
            
            return UICollectionReusableView()
        }
        
//        unsafeAddressOf()
        
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        
        return CGSizeMake(CMDB_Screen_Width, 44)
    }
}
